package com.bsoft.hospitalchs.ui.childinfo;

import android.widget.ImageView;
import android.widget.TextView;

import com.bsoft.hospitalchs.R;
import com.bsoft.hospitalchs.adapter.RecordListAdapter;
import com.bsoft.hospitalchs.base.BaseFragment;
import com.bsoft.hospitalchs.model.ChildRecordList;
import com.bsoft.hospitalchs.model.RecordInfo;
import com.bsoft.hospitalchs.model.SearchResBean;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.wuxiaolong.pullloadmorerecyclerview.PullLoadMoreRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * Created by 泅渡者
 * Created on 2017/5/18.
 * 儿童预检记录
 */

public class ChildinfoFragment extends BaseFragment<ChildinfoContract.Presenter> implements ChildinfoContract.View  , PullLoadMoreRecyclerView.PullLoadMoreListener{

    @Bind(R.id.iv_head)
    ImageView ivHead;
    @Bind(R.id.tv_name)
    TextView tvName;
    @Bind(R.id.textView2)
    TextView textView2;
    @Bind(R.id.tv_gender)
    TextView tvGender;
    @Bind(R.id.tv_account)
    TextView tvAccount;
    @Bind(R.id.tv_yue)
    TextView tvYue;
    @Bind(R.id.tv_doctor)
    TextView tvDoctor;
    @Bind(R.id.tv_date)
    TextView tvDate;
    @Bind(R.id.tv_years)
    TextView tvYears;
    public SearchResBean searchResBean;

    @Bind(R.id.pullLoadMoreRecyclerView)
    PullLoadMoreRecyclerView pullLoadMoreRecyclerView;
    private List<ChildRecordList.BodyBean> dataList;
    private RecordListAdapter recordListAdapter;
    @Override
    public int onSetLayoutId() {
        return R.layout.fragment_child_info;
    }

    @Override
    public void initView() {
        this.createPresenter(new ChildinfoPresenter(this, getActivity()));
        Gson gson = new Gson();
        searchResBean = gson.fromJson(getActivity().getIntent().getStringExtra("SearchResBean").toString(), new TypeToken<SearchResBean>() {
        }.getType());
        tvName.setText(searchResBean.getBody().getPERSONNAME());
        tvAccount.setText(searchResBean.getBody().getCARDNO());
        tvDate.setText(searchResBean.getBody().getHEALTHCAREDATE());
        tvGender.setText(searchResBean.getBody().getSEXNAME());
        tvYears.setText(searchResBean.getBody().getAGE());
        tvDoctor.setText(searchResBean.getBody().getBIRTHDAY());
        tvYue.setText(searchResBean.getBody().getADVANCECOST()+"");

        getData();
        
        
        dataList = new ArrayList<>();
        recordListAdapter = new RecordListAdapter(getActivity(), dataList);
        pullLoadMoreRecyclerView.setAdapter(recordListAdapter);
        pullLoadMoreRecyclerView.setRefreshing(true);
        pullLoadMoreRecyclerView.setColorSchemeResources(R.color.colorgreen);
        //设置上拉刷新文字
        pullLoadMoreRecyclerView.setFooterViewText("正在加载");
        //        设置上拉刷新文字颜色
        pullLoadMoreRecyclerView.setFooterViewTextColor(R.color.colorgreen);
        pullLoadMoreRecyclerView.setLinearLayout();

        pullLoadMoreRecyclerView.setOnPullLoadMoreListener(this);
      //是否设置上下拉刷新
        pullLoadMoreRecyclerView.setPullRefreshEnable(false);
        pullLoadMoreRecyclerView.setPushRefreshEnable(false);
        

    }

    private void getData() {

        RecordInfo record = new RecordInfo();
        record.setMethod("execute");
        record.setServiceId("phis.mobileService");
        record.setServiceAction("getChildClinicRecordList");
        RecordInfo.BodyBean bodyBean = new RecordInfo.BodyBean();
        bodyBean.setEMPIID(searchResBean.getBody().getEMPIID());
        record.setBody(bodyBean);
        presenter.getDate(record);

    }
    @Override
    public void showCaseList(ChildRecordList childCaseRes) {
        dataList = new ArrayList<>();
        if (childCaseRes.getBody().isEmpty()) {
            return;
        }
        for (int i = 0; i < childCaseRes.getBody().size(); i++) {
            ChildRecordList.BodyBean bean = new ChildRecordList.BodyBean();
            bean.setVISITTIME(childCaseRes.getBody().get(i).getVISITTIME());
            bean.setAGE(childCaseRes.getBody().get(i).getAGE());
            bean.setTYPE(childCaseRes.getBody().get(i).getTYPE());
            bean.setJGMC(childCaseRes.getBody().get(i).getJGMC());
            dataList.add(bean);
        }

        recordListAdapter.addAllData(dataList);
    }

    @Override
    public void stopRefresh() {
        pullLoadMoreRecyclerView.setRefreshing(false);
        pullLoadMoreRecyclerView.setPullLoadMoreCompleted();
    }
    @Override
    public void hideLoading() {


    }

    @Override
    public void showLoadingDialog() {

    }


    @Override
    public void onRefresh() {
        recordListAdapter.clearData();
        getData();

    }



    @Override
    public void onLoadMore() {
        getData();
    }

}
